package com.accesshollywood.grfxOrdering.basics;

import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

import com.accesshollywood.indexables.items.AbstractStillFootage;
import com.accesshollywood.indexables.items.FootageIndexed;
import com.accesshollywood.indexables.items.StillIndexed;
import com.accesshollywood.indexables.items.TextIndexed;

public class NamedList<IndexedSQL> extends ArrayList<IndexedSQL> {
	/*
	public enum TYPE {
		NOTE,
		FILENAME,
		TEXT,
		STILLS,
		FOOTAGE;
	}

	// public static final int TEXT = 0;
	// public static final int FOLDER = 1;

	public int			id;
	public String		name;		// use mainly as a path for stills /
									// footage folders
	public TYPE			type;
	public GrfxOrder	grfxOrder;

	public NamedList() {
		super();
		this.name = "";
	}

	public NamedList(String path) {
		super();
		this.name = path;
	}

	public NamedList(JTextArea textArea, TYPE type) {
		this(textArea.getText(), type);
	}

	public NamedList(String rawText, TYPE type) {
		super();
		this.type = type;
		this.name = "";

		IndexedSQL[] lines = (IndexedSQL[]) rawText.split("\\n");
		for (int i = 0; i < lines.length; i++) {
			IndexedSQL currLine = lines[i];
			if (currLine instanceof String) {
				switch (type) {
					case NOTE:
					case FILENAME:
					case TEXT:
						Text newText = new Text();
//						newText.setElement(currLine);
						this.add((IndexedSQL)newText);
						break;
					case STILLS:
						Still newStill = new Still();
						//newStill.description = (String) currLine;
//						newStill.setDescription((String)currLine);
						
						this.add((IndexedSQL) newStill);
						break;
					case FOOTAGE:
						Footage newFootage = new Footage();
						//newFootage.description = (String) currLine;
						
//						newFootage.setDescription((String)currLine);
						this.add((IndexedSQL) newFootage);
						break;
				}
			} else if (currLine instanceof AbstractStillFootage) {
//				String description = ((AbstractStillFootage) currLine).getDescription();
//				this.add((IndexedSQL) description);
			}
		}
	}

	public void setGrfxOrder(GrfxOrder grfxOrder) {
		this.grfxOrder = grfxOrder;
	}

	public DefaultListModel toListModel() {
		DefaultListModel model = new DefaultListModel();

		for (int i = 0; i < this.size(); i++) {
			IndexedSQL currLine = this.get(i);
			if (currLine instanceof String) {
				model.addElement(currLine);
			} else if (currLine instanceof AbstractStillFootage) {
//				String description = ((AbstractStillFootage) currLine).getDescription();
//				model.addElement((IndexedSQL) description);
			}
		}
		return model;
	}

	public PermissionableTableModel toTableModel(ArrayList<User> assignedUsers) {
		PermissionableTableModel model = new PermissionableTableModel(assignedUsers);
		model.addColumn("text");
		for (int i = 0; i < this.size(); i++) {
			IndexedSQL currLine = this.get(i);
			if (currLine instanceof String) {
				model.addRow(new Object[] { currLine });
			} else if (currLine instanceof AbstractStillFootage) {
//				String description = ((AbstractStillFootage) currLine).getDescription();
//				model.addRow(new Object[] { (IndexedSQL) description });
			}
		}
		return model;
	}

	public String toString() {
		String str = "";
		for (int i = 0; i < this.size(); i++) {
			str += this.get(i).toString();
			if (this.size() > 1 && i < this.size() - 1) {
				str += "\n";
			}
		}
		return str;
	}
	
	public String toPrint(){
		String str = "        ";
		str += toString();
		return str;
	}

	public boolean isNote() {
		return (type == TYPE.NOTE);
	}

	public boolean isText() {
		return (type == TYPE.TEXT);
	}

	public boolean isStills() {
		return (type == TYPE.STILLS);
	}

	public boolean isFootage() {
		return (type == TYPE.FOOTAGE);
	}

	public void addStr(String string) {
		this.add((IndexedSQL) string);
	}

	public class PermissionableTableModel extends DefaultTableModel {
		ArrayList<User>	assignedUsers;

		public PermissionableTableModel(ArrayList<User> assignedUsers) {
			this.assignedUsers = assignedUsers;
		}

		@Override
		public boolean isCellEditable(int row, int column) {
			boolean currUserCanEdit = false;
//			if (grfxOrder != null && grfxOrder.getAssignedUsers() != null) {
//				currUserCanEdit = grfxOrder.getAssignedUsers().contains(Users.currentUser);
//			}
			if ((Users.currentUser.core().isAdmin() || Users.currentUser.core().isOrderedBy() || Users.currentUser.core().isResearch() || Users.currentUser.core().isLegal()) && currUserCanEdit) {
				return true;
			} else {
				return false;
			}
		}
	}

	public void set(int row, String data) {
		switch (type) {
			case NOTE:
				this.set(row, (IndexedSQL) data);
				break;
			case TEXT:
				this.set(row, (IndexedSQL) data);
				break;
			case STILLS:
				Still newStill = new Still();
//				newStill.setDescription(data);
				this.set(row, (IndexedSQL) newStill);
				break;
			case FOOTAGE:
				Footage newFootage = new Footage();
//				newFootage.setDescription(data);
				this.set(row, (IndexedSQL) newFootage);
				break;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((grfxOrder == null) ? 0 : grfxOrder.hashCode());
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		NamedList other = (NamedList) obj;
		if (grfxOrder == null) {
			if (other.grfxOrder != null)
				return false;
		} else if (!grfxOrder.equals(other.grfxOrder))
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (type != other.type)
			return false;
		return true;
	}
*/
}
